package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Data;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class DataDao {

	DBHelp<Data> db=new DBHelp<Data>();
	
	public void save(Data data){
		String sql="insert into t_data(id,title,content,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		db.executeSQL(sql,data.getId(),data.getTitle(),data.getContent(),data.getCreatetime(),data.getEmployeeid(),data.getProjectid());
	}
	
	public void update(Data data){
		String sql="update t_data set title=?,content=?,createtime=? where id=?";
		db.executeSQL(sql,data.getTitle(),data.getContent(),data.getCreatetime(),data.getId());
	}
	
	public void delete(String id){
		String sql="delete from t_data where id=?";
		db.executeSQL(sql,id);
	}
	
	public Data findById(String id){
		String sql="select * from t_data where id=?";
		return db.queryForObject(sql,new DRowMapper(),id);
	}
	
	public List<Data> findByProjectid(String projectid){
		String sql="select * from t_data where projectid=? order by createtime desc";
		return db.queryForList(sql,new DRowMapper(),projectid);
	}
	
	private class DRowMapper implements RowMapper<Data>{

		public Data mapperRow(ResultSet rs) throws SQLException {
			
			Data data=new Data();
			data.setId(rs.getString("id"));
			data.setTitle(rs.getString("title"));
			data.setContent(rs.getString("content"));
			data.setCreatetime(rs.getString("createtime"));
			data.setEmployeeid(rs.getInt("employeeid"));
			data.setProjectid(rs.getString("projectid"));
			return data;
			
		}
		
	}
}
